﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using formautenticacao = BD2_5479_5513.FormAutenticacao;

using memoria_partilhada = BD2_5479_5513.SharedMemory;
using dbcontrol = BD2_5479_5513.DatabaseControl;

namespace BD2_5479_5513
{
    public partial class FormAluno : Form
    {
        public FormAluno()
        {
            InitializeComponent();
            preenchimentoInicial();
        }

        public void preenchimentoInicial()
        {
            memoria_partilhada mem = new memoria_partilhada();
            dbcontrol bd = new dbcontrol();
            var cod_utilizador = mem.codUtilizador;
            var lista_inscrito = bd.getDisciplinasInscrito(cod_utilizador);
            var lista_por_inscrever = bd.getDisciplinasNaoInscrito(cod_utilizador);

            foreach (var item in lista_inscrito)
            {
                this.listBox1.Items.Add(item[1].ToString() + " - " + item[2].ToString());
            }

            for (int i = 0; i < lista_por_inscrever.Count(); i++)
			{
			    Label label_test = new Label();
                label_test.Text = lista_por_inscrever[i][1].ToString() + " - " + lista_por_inscrever[i][2].ToString();
                label_test.AutoSize = true;

                Button button_inscrever = new Button();
                button_inscrever.Text = "Inscrever";
                button_inscrever.Tag = lista_por_inscrever[i][0].ToString();
                button_inscrever.Click += new System.EventHandler(this.button_inscrever_Click);

                this.tableLayoutPanel2.Controls.Add(label_test, 0, i);
                this.tableLayoutPanel2.Controls.Add(button_inscrever, 1, i);
			}

            this.tabPageAvaliacoes.Controls.Remove(this.tableLayoutPanel1);

            var mensagem = mem.mensagem;
            this.toolStripStatusLabel2.Text = mensagem;

            var nome = mem.nome_utilizador;
            this.toolStripStatusLabel4.Text = nome;
            
        }

        public void preenchimentoAlterarInfo()
        {
            memoria_partilhada mem = new memoria_partilhada();
            dbcontrol bd = new dbcontrol();
            var cod_utilizador = mem.codUtilizador;
            var lista_inscrito = bd.getDisciplinasInscrito(cod_utilizador);

            this.comboBox1.Items.Clear();
            this.listBox1.Items.Clear();

            this.tabPageDisciplinas.Controls.Remove(this.tableLayoutPanel2);

            TableLayoutPanel tableLayoutPaneltest = new TableLayoutPanel();
            tableLayoutPaneltest.AutoScroll = true;
            tableLayoutPaneltest.ColumnCount = 2;
            tableLayoutPaneltest.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
            tableLayoutPaneltest.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
            tableLayoutPaneltest.Location = new System.Drawing.Point(12, 184);
            tableLayoutPaneltest.Name = "tableLayoutPanel2";
            tableLayoutPaneltest.RowCount = 2;
            tableLayoutPaneltest.RowStyles.Add(new System.Windows.Forms.RowStyle());
            tableLayoutPaneltest.RowStyles.Add(new System.Windows.Forms.RowStyle());
            tableLayoutPaneltest.Size = new System.Drawing.Size(619, 81);
            tableLayoutPaneltest.TabIndex = 2;

            this.tabPageDisciplinas.Controls.Add(tableLayoutPaneltest);
      
            foreach (var item in lista_inscrito)
            {
                //this.comboBox1.Items.Add(item[2].ToString());
                this.listBox1.Items.Add(item[2].ToString());
            }

            var lista_por_inscrever = bd.getDisciplinasNaoInscrito(cod_utilizador);

            for (int i = 0; i < lista_por_inscrever.Count(); i++)
            {
                Label label_test = new Label();
                label_test.Text = lista_por_inscrever[i][1].ToString() + " - " + lista_por_inscrever[i][2].ToString();
                label_test.AutoSize = true;

                
                Button button_inscrever = new Button();
                button_inscrever.Text = "Inscrever";
                button_inscrever.Tag = lista_por_inscrever[i][0].ToString();
                button_inscrever.Click += new System.EventHandler(this.button_inscrever_Click);


                tableLayoutPaneltest.Controls.Add(label_test, 0, i);
                tableLayoutPaneltest.Controls.Add(button_inscrever, 1, i);
                
            }

            var nome = mem.nome_utilizador;
            this.toolStripStatusLabel4.Text = nome;

        }

        public static void ThreadProcAutenticacao()
        {
            Application.Run(new formautenticacao());
        }

        private void button_terminar_sessao_aluno_Click(object sender, EventArgs e)
        {
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProcAutenticacao));
            t.Start();
            this.Close();
        }

        private void button_sair_aluno_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void button_inscrever_Click(object sender, EventArgs e)
        {
            memoria_partilhada mem = new memoria_partilhada();
            dbcontrol bd = new dbcontrol();

            Button btn = sender as Button;
            int cod_disciplina;

            if (btn != null)
            {
                cod_disciplina = Int32.Parse(btn.Tag.ToString());
            }
            else
                cod_disciplina = 0;
            
            //meter o metodo do databasecontrol aqui
            //fazer o triger para só deixar inscrever nas disciplinas do curso

            var mensagem = bd.inscrever_disciplina(mem.codUtilizador, cod_disciplina);
            this.toolStripStatusLabel2.Text = mensagem;

            preenchimentoAlterarInfo();
            
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            // 
            // label Tipo
            //
            Label label_tipo = new Label();
            label_tipo.AutoSize = true;
            label_tipo.Location = new System.Drawing.Point(3, 0);
            label_tipo.Name = "label2";
            label_tipo.Size = new System.Drawing.Size(28, 13);
            label_tipo.TabIndex = 0;
            label_tipo.Text = "Tipo";
            // 
            // label Data
            //
            Label label_data = new Label();
            label_data.AutoSize = true;
            label_data.Location = new System.Drawing.Point(156, 0);
            label_data.Name = "label3";
            label_data.Size = new System.Drawing.Size(30, 13);
            label_data.TabIndex = 1;
            label_data.Text = "Data";
            // 
            // label sala
            //
            Label label_sala = new Label();
            label_sala.AutoSize = true;
            label_sala.Location = new System.Drawing.Point(399, 0);
            label_sala.Name = "label6";
            label_sala.Size = new System.Drawing.Size(28, 13);
            label_sala.TabIndex = 4;
            label_sala.Text = "Sala";
            // 
            // label estado
            // 
            Label label_estado = new Label();
            label_estado.AutoSize = true;
            label_estado.Location = new System.Drawing.Point(455, 0);
            label_estado.Name = "label7";
            label_estado.Size = new System.Drawing.Size(101, 13);
            label_estado.TabIndex = 5;
            label_estado.Text = "Estado de Inscrição";
            // 
            // label hora
            // 
            Label label_hora = new Label();
            label_hora.AutoSize = true;
            label_hora.Location = new System.Drawing.Point(258, 0);
            label_hora.Name = "label4";
            label_hora.Size = new System.Drawing.Size(30, 13);
            label_hora.TabIndex = 2;
            label_hora.Text = "Hora";
            // 
            // label peso
            // 
            Label label_peso = new Label();
            label_peso.AutoSize = true;
            label_peso.Location = new System.Drawing.Point(343, 0);
            label_peso.Name = "label5";
            label_peso.Size = new System.Drawing.Size(31, 13);
            label_peso.TabIndex = 3;
            label_peso.Text = "Peso";
            // 
            // label nota
            //
            Label label_nota = new Label();
            label_nota.AutoSize = true;
            label_nota.Location = new System.Drawing.Point(562, 0);
            label_nota.Name = "label8";
            label_nota.Size = new System.Drawing.Size(30, 13);
            label_nota.TabIndex = 6;
            label_nota.Text = "Nota";

            TableLayoutPanel tableLayoutAvaliacoes = new TableLayoutPanel();
            tableLayoutAvaliacoes.AutoScroll = true;
            tableLayoutAvaliacoes.AutoSize = true;
            tableLayoutAvaliacoes.ColumnCount = 7;
            tableLayoutAvaliacoes.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 30F));
            tableLayoutAvaliacoes.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 20F));
            tableLayoutAvaliacoes.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 16.66667F));
            tableLayoutAvaliacoes.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 11.11111F));
            tableLayoutAvaliacoes.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 11.11111F));
            tableLayoutAvaliacoes.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle());
            tableLayoutAvaliacoes.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 11.11111F));
            tableLayoutAvaliacoes.Controls.Add(label_tipo, 0, 0);
            tableLayoutAvaliacoes.Controls.Add(label_data, 1, 0);
            tableLayoutAvaliacoes.Controls.Add(label_sala, 4, 0);
            tableLayoutAvaliacoes.Controls.Add(label_estado, 5, 0);
            tableLayoutAvaliacoes.Controls.Add(label_hora, 2, 0);
            tableLayoutAvaliacoes.Controls.Add(label_peso, 3, 0);
            tableLayoutAvaliacoes.Controls.Add(label_nota, 6, 0);
            tableLayoutAvaliacoes.Location = new System.Drawing.Point(11, 36);
            tableLayoutAvaliacoes.Margin = new System.Windows.Forms.Padding(0);
            tableLayoutAvaliacoes.Name = "tableLayoutPanel1";
            tableLayoutAvaliacoes.RowCount = 2;
            tableLayoutAvaliacoes.RowStyles.Add(new System.Windows.Forms.RowStyle());
            tableLayoutAvaliacoes.RowStyles.Add(new System.Windows.Forms.RowStyle());
            tableLayoutAvaliacoes.Size = new System.Drawing.Size(620, 152);
            tableLayoutAvaliacoes.TabIndex = 4;

            this.tabPageAvaliacoes.Controls.Add(tableLayoutAvaliacoes);
        }

        private void button_anular_inscricao_Click(object sender, EventArgs e)
        {

            memoria_partilhada mem = new memoria_partilhada();

            string texto_seleccionado = "";

            for (int i = 0; i < this.listBox1.Items.Count; i++)
            {
                if (this.listBox1.GetSelected(i) == true)
                {
                    texto_seleccionado = this.listBox1.Items[i].ToString();
                }
            }

            var lista_inscrito = mem.listaInscrito;

            var id_disciplina = "";
            var cod_aluno = mem.codUtilizador;

            foreach (var item in lista_inscrito)
            {
                if(texto_seleccionado.Equals(item[2].ToString()))
                    id_disciplina = item[0].ToString();
            }

            // fazer aqui cena do desinscrever

        }
    }
}
